package pl.bizuteria.sklep.admin.client.activity.mapper;

import pl.bizuteria.sklep.admin.client.activity.bracelet.BraceletEditActivity;
import pl.bizuteria.sklep.admin.client.activity.earing.EaringCreateActivity;
import pl.bizuteria.sklep.admin.client.activity.earing.EaringEditActivity;
import pl.bizuteria.sklep.admin.client.place.ProxyCreatePlace;
import pl.bizuteria.sklep.admin.client.place.ProxyEditPlace;
import pl.bizuteria.sklep.admin.client.ui.product.bracelet.BraceletEditView;
import pl.bizuteria.sklep.admin.client.ui.product.earing.EaringEditView;
import pl.bizuteria.sklep.api.shared.proxy.BraceletProxy;
import pl.bizuteria.sklep.api.shared.proxy.EaringProxy;
import pl.bizuteria.sklep.api.shared.request.ApplicationRequestFactory;
import pl.bizuteria.sklep.api.shared.request.EaringRequest;

import com.google.gwt.activity.shared.Activity;
import com.google.gwt.activity.shared.ActivityMapper;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceController;
import com.google.inject.Inject;
import com.google.web.bindery.requestfactory.shared.EntityProxyId;

public class CreateOrEditActivityMapper implements ActivityMapper {

	
	private EaringEditView earingEditView;
	private EventBus eventBus;
	private ApplicationRequestFactory requestFactory;
	private PlaceController placeController;
	private BraceletEditView braceletEditView;

	@Inject
	public CreateOrEditActivityMapper(EventBus eventBus,
			EaringEditView earingEditView, 
			PlaceController placeController,
			BraceletEditView braceletEditView,
			ApplicationRequestFactory requestFactory) {
		super();
		this.eventBus = eventBus;
		this.earingEditView = earingEditView;
		this.placeController = placeController;
		this.braceletEditView = braceletEditView;
		this.requestFactory = requestFactory;
	}
	
	public Activity getActivity(Place place) {

		if (place instanceof ProxyEditPlace) {
			ProxyEditPlace proxyEditPlace = (ProxyEditPlace) place;
			if (EaringProxy.class.equals(proxyEditPlace.getStableId()
					.getProxyClass())) {
				return new EaringEditActivity(earingEditView, (EntityProxyId<EaringProxy>) proxyEditPlace.getStableId(), eventBus, placeController, requestFactory.earingRequest(), requestFactory);

			} else if (BraceletProxy.class.equals(proxyEditPlace.getStableId()
					.getProxyClass())) {

				return new BraceletEditActivity(braceletEditView,  (EntityProxyId<BraceletProxy>) proxyEditPlace.getStableId(), eventBus, placeController, requestFactory.braceletRequest(), requestFactory);
			}
		}
		else if (place instanceof ProxyCreatePlace){
			ProxyCreatePlace proxyCreatePlace = (ProxyCreatePlace) place;
			if (EaringProxy.class.equals(proxyCreatePlace.getProxyType())){
				final EaringRequest req = requestFactory.earingRequest();
				return new EaringCreateActivity(earingEditView, eventBus, placeController, requestFactory.earingRequest(), EaringProxy.class, requestFactory);
				
			}
		}
		return null;
	}

}
